package com.googlecode.c4e.ui.actions;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionDelegate;

import com.googlecode.c4e.api.model.Project;
import com.googlecode.c4e.ui.ContinuumPlugin;
import com.googlecode.c4e.ui.views.BuildHistoryView;

public class ViewBuildHistoryAction extends ActionDelegate {

	private Project project;

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		if (selection != null && selection instanceof IStructuredSelection) {
			IStructuredSelection isel = (IStructuredSelection) selection;
			if (isel.getFirstElement() instanceof Project) {
				this.project = (Project) isel.getFirstElement();
			}
		}
	}

	@Override
	public void run(IAction action) {
		if (project != null) {
			try {
				BuildHistoryView view = (BuildHistoryView) PlatformUI
						.getWorkbench().getActiveWorkbenchWindow()
						.getActivePage().showView(BuildHistoryView.VIEW_ID);
				if (view != null) {
					view.showHistoryFor(project);
				}
			} catch (Exception e) {
				ContinuumPlugin.getDefault().log("openInBrowser", e);
				throw new RuntimeException(e.getMessage(), e);
			}
		}
	}
}
